
//
//  TabBar_VC.m
//  U_Airplane
//
//  Created by iOS on 2018/1/9.
//  Copyright © 2018年 cj. All rights reserved.
//

#import "TabBar_VC.h"
#import "CustomTabBar.h"
#import "Home_VC.h"
#import "ClassifyMain_VC.h"
#import "News_VC.h"
#import "ShoppingCar_VC.h"
#import "PersonCenter_VC.h"


#define kClassKey   @"rootVCClassString"
#define kTitleKey   @"title"
#define kImgKey     @"imageName"
#define kSelImgKey  @"selectedImageName"

@interface TabBar_VC ()<UITabBarControllerDelegate,UIGestureRecognizerDelegate>

@property (nonatomic, assign) NSInteger lastTabbarIndex;

@property (nonatomic, strong) UITabBar *tabbarVC;

@property (nonatomic, strong)CustomTabBar *tabbar;
@property (nonatomic, assign)NSInteger staticIndex;

@end

@implementation TabBar_VC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tabBar.barTintColor = [UIColor whiteColor];
    // 初始化所有控制器
    [self setUpChildVC];
    self.selectedIndex = 0;
}
#pragma mark -初始化所有控制器

- (void)setUpChildVC {
    
    Home_VC *homeVC = [[Home_VC alloc] init];
    [self setChildVC:homeVC title:@"首页" image:@"tab_bar_button_home_1" selectedImage:@"tab_bar_button_home_2"];
    
    
    ClassifyMain_VC *classifyVC = [[ClassifyMain_VC alloc] init];
    [self setChildVC:classifyVC title:@"分类" image:@"tab_bar_button_more_1" selectedImage:@"tab_bar_button_more_2"];
    
    News_VC *newsVC = [[News_VC alloc]init];
    [self setChildVC:newsVC title:nil image:@"tab_bar_button_news" selectedImage:@"tab_bar_button_news"];
    
    
    ShoppingCar_VC *carVc = [[ShoppingCar_VC alloc] init];
    [self setChildVC:carVc title:@"购物车" image:@"tab_bar_button_shopping_1" selectedImage:@"tab_bar_button_shopping_2"];
    
    PersonCenter_VC  *myVC = [[PersonCenter_VC alloc] init];
    [self setChildVC:myVC title:@"我的" image:@"tab_bar_button_mine_1" selectedImage:@"tab_bar_button_mine_2"];
    
    
    
}

- (void) setChildVC:(UIViewController *)childVC title:(NSString *) title image:(NSString *) image selectedImage:(NSString *) selectedImage {
    
    childVC.tabBarItem.title = title;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"54cc54"];
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    [childVC.tabBarItem setTitleTextAttributes:dict forState:UIControlStateNormal];
    childVC.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    BaseNavController *nav = [[BaseNavController alloc] initWithRootViewController:childVC];
    //nav.navigationBar.backgroundColor = [UIColor blackColor];
    nav.navigationBar.barTintColor = [UIColor blackColor];
    [self addChildViewController:nav];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    NSLog(@"item name = %@", item.title);
    NSInteger index = [self.tabBar.items indexOfObject:item];
    [self animationWithIndex:index];
    if([item.title isEqualToString:@""])
    {
        // 也可以判断标题,然后做自己想做的事<img alt="得意" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/proud.gif" />
    }
}
- (void)animationWithIndex:(NSInteger) index {
    NSMutableArray * tabbarbuttonArray = [NSMutableArray array];
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabbarbuttonArray addObject:tabBarButton];
        }
    }
    CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration = 0.2;
    pulse.repeatCount= 1;
    pulse.autoreverses= YES;
    pulse.fromValue= [NSNumber numberWithFloat:0.7];
    pulse.toValue= [NSNumber numberWithFloat:1.3];
    [[tabbarbuttonArray[index] layer]
     addAnimation:pulse forKey:nil];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
